package com.xzcedu.mhl.view;

import com.xzcedu.mhl.domain.*;
import com.xzcedu.mhl.service.BillService;
import com.xzcedu.mhl.service.DiningTableService;
import com.xzcedu.mhl.service.EmployeeService;
import com.xzcedu.mhl.service.MenuService;
import com.xzcedu.mhl.utils.Utility;

import javax.rmi.CORBA.Util;
import java.util.List;

/**
 * @author 徐州城
 * @version 1.0
 * 满汉楼主界面
 */
public class MHLView {

    //控制是否退出菜单
    private boolean loop = true;
    private String key = ""; //接受用户的选择
    //定义EmployeeService
    private EmployeeService employeeService = new EmployeeService();
    //定义DiningTableService
    private DiningTableService diningTableService = new DiningTableService();
    //定义MenuService
    MenuService menuService = new MenuService();
    //顶一个BillService属性
    private BillService billService = new BillService();

    public static void main(String[] args) {
        new MHLView().mainMenu();
    }


    //显示一级菜单
    private void firstLevelMenu() {
        System.out.println("=============满汉楼=============");
        System.out.println("\t\t 1 登录满汉楼");
        System.out.println("\t\t 2 退出满汉楼");
        System.out.print("请输入你的选择: ");
    }

    //显示二级菜单
    private void secondLevelMenu() {
        System.out.println("\n=============满汉楼(二级菜单)=============");
        System.out.println("\t\t 1 显示餐桌状态");
        System.out.println("\t\t 2 预定餐桌状态");
        System.out.println("\t\t 3 显示所有菜品");
        System.out.println("\t\t 4 点餐服务");
        System.out.println("\t\t 5 查看账单");
        System.out.println("\t\t 6 结账");
        System.out.println("\t\t 9 退出满汉楼\n");
    }

    //显示二级菜单的选择，并且实现功能
    private void secondLevelMenuChoice(String key) {
        switch (key) {
            case "1":
                diningTableService.listChange(); //显示餐桌状态
                break;
            case "2":
                orderDiningTable02();
                //orderDiningTable(); //预定餐桌状态
                break;
            case "3":
                //menuService.showAllMenu();
                listMenu();//显示所有菜品
                break;
            case "4":
                orderMenu();//点餐服务
                break;
            case "5":
                showBill();//查看账单
                break;
            case "6":
                payBill();//结账
                break;
            case "9":
                loop = false;
//                System.out.println("退出");
                break;
            default:
                System.out.println("没有该选择!!!(请重新输入)");
                break;
        }
    }

    //完成订座(预定餐桌)
    public void orderDiningTable() {
        System.out.println("\n=============预定餐桌=============");
        System.out.print("请选择要预定的餐桌编号(-1退出): ");
        int orderId = Utility.readInt(); //读取数字
        if (orderId == -1) {
            System.out.println("=============取消预订餐桌=============\n");
            return;
        }
        //该方法得到的是Y or N
        char key = Utility.readConfirmSelection();//key为Y or N
        if (key != 'Y') {//当key不等于Y时，表示退出
            System.out.println("=============取消预订餐桌=============\n");
            return;
        }

        //根据orderId返回 对应的DiningTable对象，如果为null，说明该对象不存在
        DiningTable diningTable = diningTableService.getDiningTableById(orderId);//orderId 为上面输入的 orderId
        if (diningTable == null) {//不存在
            System.out.println("=============预定餐桌不存在=============\n");
            return;
        }
        //判断改餐桌的状态是否为 "空"状态
        if (!("空".equals(diningTable.getState()))) { //说明当前餐桌不是 "空"状态
            System.out.println("=============餐桌已被使用=============\n");
            return;
        }

        //接收预定信息
        System.out.print("预定人姓名: ");
        String orderName = Utility.readString(50);
        System.out.print("预定人电话: ");
        String orderTel = Utility.readString(50);
        //更新餐桌状态
        if (diningTableService.orderDiningTable(orderId, orderName, orderTel)) {
            System.out.println("=============预定餐桌成功=============\n");
        } else
            System.out.println("=============预定餐桌失败=============\n");
    }

    //完成订座(第二种方式)
    @SuppressWarnings("all")
    private void orderDiningTable02() {
        //得到所有可以使用的id对应的餐桌号，即餐桌号为空
        List<Integer> ids = diningTableService.getDiningTableAllAvailableId();
        System.out.println("\n============当前可用餐桌号如下============");
        for (Integer id : ids) { //遍历集合
            System.out.print(id + "号" + "\t");
        }

        System.out.print("\n请选择要预定的餐桌编号(-1退出): ");
        int orderId = Utility.readInt(); //读取数字
        if (orderId == -1) {
            System.out.println("=============取消预订餐桌=============\n");
            return;
        }
        //该方法得到的是Y or N
        char key = Utility.readConfirmSelection();//key为Y or N
        if (key != 'Y') {//当key不等于Y时，表示退出
            System.out.println("=============取消预订餐桌=============\n");
            return;
        }

        //根据orderId返回 对应的DiningTable对象，如果为null，说明该对象不存在
        DiningTable diningTable = diningTableService.getDiningTableById(orderId);//orderId 为上面输入的 orderId
        if (diningTable == null) {//不存在
            System.out.println("=============预定餐桌不存在=============\n");
            return;
        }
        //判断改餐桌的状态是否为 "空"状态
        if (!("空".equals(diningTable.getState()))) { //说明当前餐桌不是 "空"状态
            System.out.println("=============餐桌已被使用=============\n");
            return;
        }

        //接收预定信息
        System.out.print("预定人姓名: ");
        String orderName = Utility.readString(50);
        System.out.print("预定人电话: ");
        String orderTel = Utility.readString(50);
        //更新餐桌状态
        if (diningTableService.orderDiningTable(orderId, orderName, orderTel)) {
            System.out.println("=============预定餐桌成功=============\n");
        } else
            System.out.println("=============预定餐桌失败=============\n");
    }

    //显示所有菜品
    private void listMenu() {
        List<Menu> list = menuService.list();
        System.out.println("\n菜品编号\t\t菜品名\t\t类别\t\t价格");
        for (Menu m : list) {
            System.out.println(m);
        }
        System.out.println("=============显示完毕=============");
    }

    //完成点餐
    public void orderMenu() {
        System.out.println("=============点餐服务=============");
        System.out.print("请输入点餐的桌号(-1退出): ");
        int orderDiningTableId = Utility.readInt();
        if (orderDiningTableId == -1) {
            System.out.println("=============取消点餐=============");
            return;
        }
        //验证餐桌是否存在.
        DiningTable diningTable = diningTableService.getDiningTableById(orderDiningTableId);
        if (diningTable == null) {//说明餐桌号为空
            System.out.println("=============餐桌号不存在=============");
            return;
        }

        System.out.print("请输入点餐的菜品号(-1退出): ");
        int orderMenuId = Utility.readInt();
        if (orderMenuId == -1) {
            System.out.println("=============取消点餐=============");
            return;
        }
        //验证菜品编号是否正确
        Menu menu = menuService.getMenuById(orderMenuId);
        if (menu == null) {
            System.out.println("=============菜品不存在=============");
            return;
        }


        System.out.print("请输入点餐的菜品量(-1退出): ");
        int orderNums = Utility.readInt();
        if (orderNums == -1) {
            System.out.println("=============取消点餐=============");
            return;
        }

        //点餐
        if (billService.orderMenu(orderMenuId, orderNums, orderDiningTableId)) {
            System.out.println("=============点餐成功=============");
        } else {
            System.out.println("=============点餐失败=============");
        }
    }

    //显示账单
    public void showBill() {

//        List<Bill> allBill = billService.getAllBill();
//        System.out.println(String.format("%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t\t\t\t\t\t%s", "编号", "菜品号", "菜品量", "金额", "桌号", "日期", "状态"));
//        //遍历输出
//        for (Bill bill : allBill) {
//            System.out.println(bill);
//        }
//        System.out.println("=============显示完毕=============");

        List<MultiTableBean> multiTableBeans = billService.getAllBill02();
        System.out.println(String.format(
                "%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t%s\t\t\t\t\t\t\t%s\t\t%s\t\t%s", "编号", "菜品号", "菜品量", "金额", "桌号", "日期", "状态","菜品名","菜品价格"));
        //遍历输出
        for (MultiTableBean multiTableBean : multiTableBeans) {
            System.out.println(multiTableBean);
        }
        System.out.println("=============显示完毕=============");
    }

    //完成结账
    public void payBill() {
        System.out.println("=============结账服务=============");
        System.out.print("请选择要结账的餐桌编号(-1退出): ");
        int diningTableId = Utility.readInt();
        if (diningTableId == -1) {
            System.out.println("=============取消结账=============");
            return;
        }
        //验证餐桌是否存在
        DiningTable diningTable = diningTableService.getDiningTableById(diningTableId);
        if (diningTable == null) {
            System.out.println("=============餐桌不存在=============");
            return;
        }
        //验证餐桌是否需要结账的菜单
        if (!billService.hasPayBillByDiningTableId(diningTableId)) {
            System.out.println("=============餐桌没有需要结账的账单=============");
            return;
        }

        System.out.print("结账方式(现金/支付宝/微信)回车退出: ");
        String payMode = Utility.readString(20, "");//如果直接回车，就是返回 ""
        if ("".equals(payMode)) {
            System.out.println("=============取消结账=============");
        }

//        System.out.print("确认结账(Y/N): ");
        char key = Utility.readConfirmSelection();
        if (key == 'N') {
            System.out.println("=============取消结账=============");
            return;
        }

        //结账
        if (billService.payBill(diningTableId, payMode)) {
            System.out.println("=============完成结账=============");
        } else {
            System.out.println("=============结账失败=============");
        }
    }


    //显示主菜单
    public void mainMenu() {

        while (loop) {
            //输出一级菜单
            firstLevelMenu();

            key = Utility.readString(1);
            switch (key) {
                case "1":
                    System.out.print("请输入员工号: ");
                    String empId = Utility.readString(50);
                    System.out.print("请输入密  码: ");
                    String pwd = Utility.readString(50);
                    //需要到数据库判断，是否存在，这个用户
                    Employee employee = employeeService.getEmployeeByIdAndPwd(empId, pwd);

                    if (employee != null) { //说明该用户存在
                        System.out.println(String.format("============登录成功[%s]============\n", employee.getName()));
                        //显示二级菜单，这里二级菜单是循环操作，所以做成while
                        while (loop) {
                            //输出二级菜单
                            secondLevelMenu();
                            System.out.print("请输入你的选择: ");
                            key = Utility.readString(1);

                            //二级菜单选择与功能
                            secondLevelMenuChoice(key);
                        }
                    } else {
                        System.out.println("登录失败");
                    }
                    break;

                case "2":
                    loop = false;
                    break;
                default:
                    System.out.println("没有该选择!!!(请重新输入)");
            }
        }

        System.out.println("你退出了满汉楼系统~");
    }
}
